package com.ren.user.controller;

import com.ren.user.entity.ReclUser;
import com.ren.user.service.ReclUserService;
import com.ren.utils.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: ReclUserController
 * @Description: TODO
 * @Author: RZY
 * @DATE: 2022/7/21 11:23
 * @Version: v1.0
 */

@Api(tags = "用户接口")
@RestController
@RequestMapping("/recl-user")
public class ReclUserController {

    @Autowired
    ReclUserService userService;

    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public R doLogin(@RequestBody ReclUser reclUser) {
        String token = userService.doLogin(reclUser);
        return R.ok()
                .data("token", token);
    }

    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public R doRegister(@RequestBody ReclUser reclUser) {
        boolean res = userService.doRegister(reclUser);
        if(res) return R.ok()
                .message("恭喜您注册成功!");
        else return R.error()
                .message("系统错误，请稍后重试");
    }

    @ApiOperation(value = "查询用户信息根据JWT令牌")
    @GetMapping("/user-detail")
    public R getReclUserByJWT(HttpServletRequest request) {
        ReclUser reclUser = userService.getReclUserByJWT(request);
        return R.ok()
                .data("reclUser", reclUser);
    }
}
